using System;
using Rewired;

namespace _00_Source.__naptime
{
	public class DynamicIconObserver : Singleton<DynamicIconObserver>
	{
		private Controller _controller;

		public static ConsoleBinding ConsoleBinding { get; private set; } = ConsoleBinding.Xbox;

		public static event Action<ConsoleBinding> OnLastDeviceChanged;

		private void Start()
		{
			try
			{

				{
					DynamicIconObserver.OnLastDeviceChanged?.Invoke(ConsoleBinding.PC);
					ConsoleBinding = ConsoleBinding.PC;
				}
			}
			catch (Exception)
			{
				DynamicIconObserver.OnLastDeviceChanged?.Invoke(ConsoleBinding.PC);
				ConsoleBinding = ConsoleBinding.PC;
			}
		}

		private void Update()
		{
			ChangeSelectorIconForCurrentDevice();
		}

		private void ChangeSelectorIconForCurrentDevice()
		{
			Controller lastActiveController = ReInput.controllers.GetLastActiveController();
			if (ReInput.players.GetPlayer(0).controllers != null && lastActiveController.type != ControllerType.Mouse)
			{
				lastActiveController = ReInput.players.GetPlayer(0).controllers.GetLastActiveController();
			}
			if (lastActiveController == _controller || lastActiveController == null)
			{
				return;
			}
			_controller = lastActiveController;
			ControllerType type = lastActiveController.type;
			string text = lastActiveController.name.ToLower();
			try
			{

			}
			catch (Exception)
			{
			}
			if (type == ControllerType.Keyboard || type == ControllerType.Mouse)
			{
				DynamicIconObserver.OnLastDeviceChanged?.Invoke(ConsoleBinding.PC);
				ConsoleBinding = ConsoleBinding.PC;
				return;
			}
			ConsoleBinding consoleBinding = ConsoleBinding.Xbox;
			ConsoleBinding consoleBinding2;
			if (ReInput.players.GetPlayer(0).controllers == null || ReInput.players.GetPlayer(0).controllers.Joysticks == null || ReInput.players.GetPlayer(0).controllers.Joysticks.Count <= 0)
			{
				_controller = ReInput.players.GetPlayer(0).controllers.GetLastActiveController();
				if (text == null)
				{
					goto IL_01ba;
				}
				if (text.Contains("xbox") || text.Contains("xinput") || text.Contains("steam"))
				{
					consoleBinding2 = ConsoleBinding.Xbox;
				}
				else if (text.Contains("nintendo") && text.Contains("joycon") && (text.Contains("right") || text.Contains("left")))
				{
					consoleBinding2 = ConsoleBinding.NintendoSingleJoyCon;
				}
				else if (text.Contains("nintendo"))
				{
					consoleBinding2 = ConsoleBinding.Nintendo;
				}
				else
				{
					if (!text.Contains("ps") && !text.Contains("playstation") && !text.Contains("sony"))
					{
						goto IL_01ba;
					}
					consoleBinding2 = ConsoleBinding.PS;
				}
				goto IL_01bd;
			}
			string text2 = ReInput.players.GetPlayer(0).controllers.Joysticks[0].name.ToLower();
			if (text2 == null)
			{
				goto IL_0291;
			}
			if ((text2.Contains("nintendo") || text.Contains("joy")) && (text2.Contains("right") || text2.Contains("left")))
			{
				consoleBinding2 = ConsoleBinding.NintendoSingleJoyCon;
			}
			else if (text2.Contains("nintendo") || text2.Contains("pro controller") || text.Contains("joy"))
			{
				consoleBinding2 = ConsoleBinding.Nintendo;
			}
			else
			{
				if (!text2.Contains("playstation") && !text2.Contains("sony"))
				{
					goto IL_0291;
				}
				consoleBinding2 = ConsoleBinding.PS;
			}
			goto IL_0294;
		IL_0294:
			consoleBinding = consoleBinding2;
			DynamicIconObserver.OnLastDeviceChanged?.Invoke(consoleBinding);
			ConsoleBinding = consoleBinding;
			return;
		IL_01bd:
			consoleBinding = consoleBinding2;
			DynamicIconObserver.OnLastDeviceChanged?.Invoke(consoleBinding);
			ConsoleBinding = consoleBinding;
			return;
		IL_01ba:
			consoleBinding2 = ConsoleBinding.PC;
			goto IL_01bd;
		IL_0291:
			consoleBinding2 = ConsoleBinding.Xbox;
			goto IL_0294;
		}
	}
}
